﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//importamos
using EventSys.BussinesEntities;
using PagedList;

namespace EventSys.Repository
{
    public class EventoRepository:MasterRepository,IEventoRepository
    {

        public IPagedList<Evento> GetAllFromEventos(int page, int size)
        {
            var query = from e in Context.Eventoes
                        .Include("TipoActividad")
                        .Include("Estado")
                        orderby e.FechaIni
                        select e;

            return query.OrderBy(e => e.FechaIni).ToPagedList(page, size);
        }

        public IPagedList<Evento> GetEventosByCriterio(string criterio, int page, int size)
        {
            //throw new NotImplementedException();

            //var query = from e in Context.Eventoes
            //                .Include("TipoActividad")
            //                .Include("Estado")
            //            where e.NomEvento.ToUpper().Contains(criterio.ToUpper())
            //            orderby e.FechaIni
            //            select e;


            if (!string.IsNullOrEmpty(criterio))
            {
                //query = query.Where(e => e.NomEvento.ToUpper().Contains(criterio.ToUpper()));
                //query = from e in query
                //        where e.NomEvento.ToUpper().Contains(criterio.ToUpper())
                //        select e;
                var query = from e in Context.Eventoes
                                .Include("TipoActividad")
                                .Include("Estado")
                            where e.NomEvento.ToUpper().Contains(criterio.ToUpper())
                            orderby e.FechaIni
                            select e;
                return query.OrderBy(e => e.FechaIni).ToPagedList(page, size);
            }
            throw new NotImplementedException();

            //return query.OrderBy(e => e.FechaIni).ToPagedList(page, size);
            //return query.SingleOrDefault();

        //public ActionResult Index(SearchViewModel model) {
            //if (!string.IsNullOrEmpty(model.SearchButton) || model.Page.HasValue) {
                //var entities = new AdventureWorksEntities();
                //var results = entities.Contacts.Where(c => c.LastName.StartsWith(model.LastName) && c.EmailAddress.StartsWith(model.EmailAddress))
                //    .OrderBy(o => o.LastName);
                //var pageIndex = model.Page ?? 0;
                //model.SearchResults = results.ToPagedList(pageIndex, 25);
            //}
            //return View(model);
        //}

        }

        public List<Evento> GetAllEventos()
        {
            var query = from e in Context.Eventoes
                            .Include("TipoActividad")
                            .Include("Estado")
                        select e;
            return query.ToList();
        }

        public List<Evento> GetEventosByCriterio(string criterio)
        {
            var query = from e in Context.Eventoes
                            .Include("TipoActividad")
                            .Include("Estado")
                        select e;

            if (criterio != null && criterio != "" && !string.IsNullOrEmpty(criterio))
            {
                query = from e in query
                        where e.NomEvento.ToUpper().Contains(criterio.ToUpper())
                        select e ;
            }

            return query.ToList();
        }

        public Evento GetEventoById(int id)
        {
            var query = Context.Eventoes.Find(id);
            //var query = from e in Context.Eventoes
            //                .Include("TipoActividad")
            //                .Include("Estado")
            //            select e;
            return query;


        }

        public List<Evento> GetEventoInvAndAuspById(int id)
        {
            var evento = from e in Context.Eventoes
                         join i in Context.Inversions on e.idEvents equals i.IdEvents
                         join da in Context.DetAuspiciadors on e.idEvents equals da.idEvento
                         where e.idEvents == id
                         select new { e, i, da };

            //var lista = evento.ToList();
            //ViewBag.ListadoDetalle = lista;
            
            //return evento;
            throw new NotImplementedException();
        }


        public void AddEvento(Evento evento)
        {
            
            Context.Eventoes.Add(evento);
            Context.SaveChanges();
        }

        public void UpdateEvento(Evento evento)
        {
            Context.Entry(evento).State = System.Data.EntityState.Modified;
            Context.SaveChanges();
        }

        public void RemoveEvento(int id)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<Evento> GetEventos()
        {
            //var contexto = new DBEventSysContext();

            return Context.Eventoes.Select(eve => 
                
                new Evento()
            {

                idEvents = eve.idEvents,
                NomEvento = eve.NomEvento,
                FechaIni = eve.FechaIni,
                FechaFin = eve.FechaFin,
                idTipoEvento = eve.idTipoEvento,
                Organizador = eve.Organizador,
                Sede = eve.Sede,
                Informes = eve.Informes,
                IngresoLibre = eve.IngresoLibre          

            });
        }       
    }
}
